<template>
	<view>
		<view class="cu-bar bg-white solid-bottom margin-top">
			<view class="action">
				<text class="cuIcon-title text-orange "></text>
			司机:{{driver.drivername}}
			</view>
		</view>
		<view class="cu-list menu-avatar">
				<view class="cu-item" v-for="(item, index) in orders" :key="index" @click="gotoText(item)" >
					<view class="cu-avatar round lg" :style="{ backgroundImage : 'url('+item.headImg+')'}"></view>
					<view class="content">
						<view class="text-grey">
							<view class="text-cut">{{item.cdyname}}-{{item.tname}}</view>
						<view class="cu-tag round bg-black sm">{{item.username}}</view>
						</view>
						<view class="text-gray text-sm flex">
							<view class="text-cut">
								{{item.address.startAddress}}
							</view>
						</view>
						<view class="text-gray text-sm flex">
							<view class="text-cut">
								{{item.address.endAddress}}
							</view>
						</view>
					</view>
					<view class="action">
						<view class="text-grey text-xs">{{item.createTime}}</view>
						<view class="cu-tag round bg-grey sm">5</view>
					</view>
				</view>
				<view class="cu-bar btn-group">
					<button class="cu-btn bg-green shadow-blur round" @click="GOtalk()">聊天室</button>
				</view>
		</view>
		<!-- 对话框 -->
		<uni-popup ref="popupDialog" type="dialog">
			<uni-popup-dialog :type="msgType" title="确认接单" :content="context" :before-close="true" @confirm="dialogConfirm" @close="dialogClose">

			</uni-popup-dialog>
		</uni-popup>
		<!-- 消息提示 -->
		<uni-popup ref="popupMessage" type="message">
			<uni-popup-message :type="msgType" :message="message" :duration="2000"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	import uniPopupMessage from '@/components/uni-popup/uni-popup-message.vue'
	import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue'
	import uniPopupShare from '@/components/uni-popup/uni-popup-share.vue'
	import socket from '@/js_sdk/plus-websocket/index.js'
export default {
	 components: {
	    uniPopupMessage,
	    uniPopupDialog,
	    uniPopupShare,
		uniPopup
	    },
	data() {
		return {
			context:"接单异常",
			msgType: 'success',
			message:"这是一条消息",
			orders: [],
			data:null,
			driver:{
				drivername:''
			},
			SocketTask:null,
			order:null,
		};
	},
	onLoad(options) {
		// #ifdef H5
		this.headerTop = document.getElementsByTagName('uni-page-head')[0].offsetHeight + 'px';
		// #endif
		this.loadData();
		if(this.SocketTask==null){
		this.SocketTask =socket.connectSocket({
			url:'ws://'+this.baseipurl+'websocket/1',
			success:(res)=> {
			}
			});	
			this.SocketTask.onOpen(function(res){
				console.log('打开了连接');
			});
		}
		var that =this;
		this.SocketTask.onMessage(function(res){
				var result1=JSON.parse(res.data);
				if(result1.code==1000){
				that.msgType = 'success';
				that.message=result1.message;
				that.$refs.popupMessage.open();
				uni.request({
					// url: 'http://localhost:9000/movehouse/BusDetails',
					url: that.baseurl + 'order/DriverOrder',
					success: res => {
						
						if (res.data.code == 20000) {
							that.orders = res.data.data;
						} else {
							uni.showToast({
								title: res.data.message,
								duration: res.data.code,
							});
								that.orders=null;
						}
					}
				});
				};
				
		});	
			
	},
	//下拉刷新
	onPullDownRefresh() {
		this.loadData('refresh');
	},
	onBackPress(){
		this.SocketTask.close();
	},
	methods: {
		loadData() {
			uni.request({
				// url: 'http://localhost:9000/movehouse/BusDetails',
				url: this.baseurl + 'order/DriverOrder',
				success: res => {
					
					if (res.data.code == 20000) {
						this.orders = res.data.data;
					} else {
						uni.showToast({
							title: res.data.message,
							duration: res.data.code,
						});
							this.orders=null;
					}
				}
			});
			//查询司机的代码
			
			uni.request({
				// url: 'http://localhost:9000/movehouse/BusDetails',
				url: this.baseurl + 'driverAddress/search',
				method: 'POST',
				dataType: 'json',
				data:{
					id:1
				},
				success: res => {
					if (res.data.code == 20000&&res.data.data.length==1) {
						this.driver = res.data.data[0];
						uni.setStorageSync('name',this.driver.drivername);
					} else {
						uni.showToast({
							title: res.data.message,
							duration: res.data.code
						});					
						}
				}
			});
			
		},
		gotoText(item){
			let orderSn = item.orderSn;
			this.order=item;
			this.data=item;
			this.context="服务类型： "+item.cdyname+"\n车辆类型： "+item.tname+"\n用户名称： "
			+item.username+"\n出发地址： "+item.address.startAddress+"\n目标地址："+item.address.endAddress
			+"\n下单时间： "+item.createTime+"\n订单编号： "+item.orderSn;
			this.$refs.popupDialog.open();
		},
		/**
		 * 对话框点击确认按钮
		 */
		dialogConfirm(done) {
			this.msgType = 'success';
			uni.request({
				// url: 'http://localhost:9000/movehouse/BusDetails',
				url: this.baseurl + 'order/DriverDoOrder',
				method:"POST",
				dataType:'json',
				data:{
					order:this.data.order,
					address:this.data.address,
					driverAddress:this.driver
				},
				success: res => {
					if (res.data.code == 20000) {
						this.message = res.data.message;
						this.msgType = 'success';
						this.loadData();
					} else {
						this.msgType = 'error'
						this.message = res.data.message;
						uni.showToast({
							title: res.data.message,
							duration: res.data.code
						});
					}
				},
				
			});
			this.$refs.popupMessage.open()
			// 需要执行 done 才能关闭对话框
			done();
			var that=this;
			uni.request({
				// url: 'http://localhost:9000/movehouse/BusDetails',
				url: that.baseurl + 'order/DriverOrder',
				success: res => {
					
					if (res.data.code == 20000) {
						that.orders = res.data.data;
					} else {
						uni.showToast({
							title: res.data.message,
							duration: res.data.code,
						});
							that.orders=null;
					}
				}
			});
		},
		/**
		 * 对话框取消按钮
		 */
		dialogClose(done) {
			this.msgType = 'info'
			this.message = '点击了对话框的取消按钮'
			this.$refs.popupMessage.open()
			// 需要执行 done 才能关闭对话框
			done()
		},
		GOtalk(){
			console.log('进入聊天室');
			let item=this.order;
			var userId=1;
			if(item.orderSn!=null&&item.orderSn!=''){
				var url='/pages/movehouse/moretalk/morepeople?orderSn='+item.orderSn+"&userId="+userId;
				console.log(url);
				uni.navigateTo({
					url:url
				});	
			}
		},
		

		
	}
};
</script>

<style></style>
